148E - Porcelain - CodeForces Solution


dp *1900

Please click on ads to support us..

Python Code:

f = lambda: map(int, input().split())
n, m = f()
u = [0]
for i in range(n):
    t = list(f())
    k = t[0]
    for i in range(1, k): t[i + 1] += t[i]
    v = [t[k]] * (k + 1)
    v[0] = t[0] = 0
    for d in range(1, k):
        v[k - d] -= min(t[j + d] - t[j] for j in range(k - d + 1))
    p = [0] * (min(m, len(u) + len(v)) + 1)
    for i, x in enumerate(u):
        for j, y in enumerate(v, i):
            if j > m: break
            p[j] = max(p[j], x + y)
    u = p
print(u[m])

C++ Code:

#include <bits/stdc++.h>

using namespace std;
using i64 = long long;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout.setf(ios::fixed), cout.precision(8);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> a(n);
    for (int i = 0; i < n; i++) {
        int c;
        cin >> c;
        a[i].resize(c + 1);
        a[i].assign(c + 1, 1E5);
        a[i][0] = 0;
        vector<vector<int>> b(c + 1, vector<int> (c + 1));
        for (int j = 1; j <= c; j++) {
            int x;
            cin >> x;
            a[i][0] += x;
            for (int k = 1; k <= j; k++) {
                b[j][k] = x + b[j - 1][k - 1];
                a[i][c - k] = min(a[i][c - k], b[j][k]);
            }
        }
        for (int j = 1; j <= c; j++) {
            a[i][j] = a[i][0] - a[i][j];
        }
        a[i][c] = a[i][0];
        a[i][0] = c;
    } 

    vector<int> dp(m + 1);
    for (int i = 0; i < n; i++) {
        for (int j = m; j >= 0; j--) {
            for (int k = 1; k <= min(j, a[i][0]); k++) {
                dp[j] = max(dp[j], dp[j - k] + a[i][k]);
            }
        }
    }

    cout << dp[m];

    return 0;
}


Comments

Submit
0 Comments
More Questions

1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter
151A - Soft Drinking
1352A - Sum of Round Numbers
281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year